package com.esun.cheetah.view.odds.adapter;

import java.util.ArrayList;
import java.util.Hashtable;

import com.esun.cheetah.R;
import com.esun.cheetah.view.personal.sportlotteryfootball.ImageListCache;

import com.esun.util.log.EDebug;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;

import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 
 * @Package com.esun.cheetah.view.personal.sportlotteryfootball.
 *          LotteryInfoExpendsAdapter
 * @ClassName: LotteryInfoExpendsAdapter
 * @Description: TODO(expend list view adapter)
 * @author handy <br/>
 * @date create at 2012-7-31 下午2:49:06
 * 
 */
public class EurOddsActInfoExpendsAdapter extends BaseExpandableListAdapter {

	private ArrayList<Hashtable<String, String>> group = new ArrayList<Hashtable<String, String>>();
	private ArrayList<ArrayList<Hashtable<String, String>>> child = new ArrayList<ArrayList<Hashtable<String, String>>>();

	private ArrayList<View> groupView = new ArrayList<View>();
	private ArrayList<ArrayList<View>> childView = new ArrayList<ArrayList<View>>();

	private LayoutInflater listInflater;

	public ImageListCache cacheUtil;

	private Context context;

	@SuppressWarnings("unused")
	private String lotid;

	public EurOddsActInfoExpendsAdapter(Context context,
			ArrayList<Hashtable<String, String>> groupContent,
			ArrayList<View> groupContentView,
			ArrayList<ArrayList<Hashtable<String, String>>> childContent,
			ArrayList<ArrayList<View>> childContentView, String lotid) {

		this.group = groupContent;
		this.child = childContent;
		this.groupView = groupContentView;
		this.childView = childContentView;
		this.listInflater = LayoutInflater.from(context);
		this.context = context;
		this.lotid = lotid;
		intHandler();

	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return child.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public long getCombinedChildId(long groupId, long childId) {
		// TODO Auto-generated method stub
		return super.getCombinedChildId(groupId, childId);
	}

	@Override
	public long getCombinedGroupId(long groupId) {
		// TODO Auto-generated method stub
		return super.getCombinedGroupId(groupId);
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		if (childView.get(groupPosition).size() < childPosition + 1) {

			// convertView = (RelativeLayout) listInflater.inflate(
			// R.layout.odds_daxiaoqiu_item, null);
			convertView = (RelativeLayout) listInflater.inflate(
					R.layout.odds_eru_item, null);

			// ImageView imgHome = (ImageView)
			// convertView.findViewById(R.id.lotteryinfo_listtable_football_image_homeTeam);
			// ImageView imgVisit = (ImageView)
			// convertView.findViewById(R.id.lotteryinfo_listtable_football_image_visitingTeam);
			((TextView) convertView
					.findViewById(R.id.tv_match_list_score_item_league_name))
					.setText(child.get(groupPosition).get(childPosition)
							.get("matchnum"));
			// ((TextView) convertView
			// .findViewById(R.id.lotteryinfo_listtable_football_tv_homeName))
			// .setText(child.get(groupPosition).get(childPosition)
			// .get("hometeam"));
			// ((TextView) convertView
			// .findViewById(R.id.lotteryinfo_listtable_football_tv_rangqiu))
			// .setText(child.get(groupPosition).get(childPosition)
			// .get("rangqiu"));
			// ((TextView) convertView
			// .findViewById(R.id.lotteryinfo_listtable_football_tv_leagueName))
			// .setText(child.get(groupPosition).get(childPosition)
			// .get("leaguename"));
			// ((TextView) convertView
			// .findViewById(R.id.lotteryinfo_listtable_football_tv_visitingName))
			// .setText(child.get(groupPosition).get(childPosition)
			// .get("visitingteam"));
			// ((TextView) convertView
			// .findViewById(R.id.tv_match_list_score_item_host_team_score))
			// .setText(child.get(groupPosition).get(childPosition)
			// .get("homescore"));
			// ((TextView) convertView
			// .findViewById(R.id.tv_match_list_score_item_guest_team_score))
			// .setText(child.get(groupPosition).get(childPosition)
			// .get("awayscore"));
			// ((TextView) convertView
			// .findViewById(R.id.lotteryinfo_listtable_football_tv_result))
			// .setText(child.get(groupPosition).get(childPosition)
			// .get("resultspf"));
			// ((TextView) convertView
			// .findViewById(R.id.tv_match_list_score_item_host_team_number))
			// .setText(child.get(groupPosition).get(childPosition)
			// .get("homestanding"));
			// ((TextView) convertView
			// .findViewById(R.id.tv_match_list_score_item_guest_team_number))
			// .setText(child.get(groupPosition).get(childPosition)
			// .get("awaystanding"));
			// ImageView imgResult = (ImageView)
			// convertView.findViewById(R.id.lotteryinfo_listtable_football_img_result);
			// if
			// (child.get(groupPosition).get(childPosition).get("result").indexOf("胜")
			// > 0) {
			// imgResult.setBackgroundResource(R.drawable.icon_default_information_win_80x60);
			// } else if
			// (child.get(groupPosition).get(childPosition).get("result").indexOf("平")
			// > 0) {
			// imgResult.setBackgroundResource(R.drawable.icon_default_information_tie_80x60);
			// } else if
			// (child.get(groupPosition).get(childPosition).get("result").indexOf("负")
			// > 0) {
			// imgResult.setBackgroundResource(R.drawable.icon_default_information_lose_80x60);
			// }

			childView.get(groupPosition).add(convertView);

			// Object[] objs = {
			// child.get(groupPosition).get(childPosition).get("hometeam_img"),
			// imgHome };

			// Object[] obj = {
			// child.get(groupPosition).get(childPosition).get("visit_img"),
			// imgVisit };

			// ArrayList<Object[]> alSending = new ArrayList<Object[]>();
			// alSending.add(obj);
			// alSending.add(objs);

			// msg = handler.obtainMessage(ADD_NEW_IMGNAME, alSending);
			// handler.sendMessage(msg);

			EDebug.o(EurOddsActInfoExpendsAdapter.this,
					"创建新的子view  groupPosition【" + groupPosition
							+ "】   childPosition【" + childPosition + "】");

			return convertView;
		} else {
			return childView.get(groupPosition).get(childPosition);
		}
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return child.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return group.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return group.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		EDebug.o(this, groupPosition);
		if (groupView.size() < groupPosition + 1) {
			convertView = (LinearLayout) listInflater.inflate(
					R.layout.odds_eur_item_title, null);
			// RelativeLayout relative = (RelativeLayout) convertView
			// .findViewById(R.id.lotteryinfo_football_listtable_relative_expand);
			TextView tvdate = (TextView) convertView
					.findViewById(R.id.sportteryfootballchooseact_tv_date);
			// TextView tvweek = (TextView) convertView
			// .findViewById(R.id.lotteryinfo_listtitle_football_tv_week);
			// TextView tvday = (TextView) convertView
			// .findViewById(R.id.lotteryinfo_listtitle_football_tv_count);
			// kael添加：根据彩种判断日期显示条的颜色
			// if (lotid.equals(Config.JINGCAIZUQIU)) {//竞彩足球 绿色
			// relative.setBackgroundResource(R.drawable.bg_default_date);

			tvdate.setTextColor(context.getResources().getColor(R.color.white));
			// tvweek.setTextColor(context.getResources().getColor(R.color.white));
			// tvday.setTextColor(context.getResources().getColor(R.color.white));
			// }
			// }else if(lotid.equals(Config.BEIJINGZUQIUDANCHANG)){//北单 绿色
			// relative.setBackgroundResource(R.drawable.btn_kjxx_group_style);
			// tvdate.setTextColor(context.getResources().getColor(R.color.white));
			// tvweek.setTextColor(context.getResources().getColor(R.color.white));
			// tvday.setTextColor(context.getResources().getColor(R.color.white));
			// }else if(lotid.equals(Config.JINGCAILANQIU)){//竞彩篮球 银色
			// relative.setBackgroundResource(R.drawable.btn_kjxx_basketball_style);
			// tvdate.setTextColor(context.getResources().getColor(R.color.solid_black));
			// tvweek.setTextColor(context.getResources().getColor(R.color.solid_black));
			// tvday.setTextColor(context.getResources().getColor(R.color.solid_black));
			// }

			// if (isExpanded) {
			// ((ImageView) convertView
			// .findViewById(R.id.lotteryinfo_football_listtable_title_btn_expand))
			// .setBackgroundDrawable(context.getResources()
			// .getDrawable(R.drawable.icon_default_packup));
			// } else {
			// ((ImageView) convertView
			// .findViewById(R.id.lotteryinfo_football_listtable_title_btn_expand))
			// .setBackgroundDrawable(context.getResources()
			// .getDrawable(R.drawable.icon_default_unfold));
			// }

			tvdate.setText(group.get(groupPosition).get("date"));
			// tvweek.setText(group.get(groupPosition).get("week"));
			// tvday.setText(child.get(groupPosition).size() + "场比赛已截止");
			groupView.add(convertView);

			return convertView;
		} else {

			// if (isExpanded) {
			// ((ImageView) groupView.get(groupPosition).findViewById(
			// R.id.lotteryinfo_football_listtable_title_btn_expand))
			// .setBackgroundDrawable(context.getResources()
			// .getDrawable(R.drawable.icon_default_packup));
			// } else {
			// ((ImageView) groupView.get(groupPosition).findViewById(
			// R.id.lotteryinfo_football_listtable_title_btn_expand))
			// .setBackgroundDrawable(context.getResources()
			// .getDrawable(R.drawable.icon_default_unfold));
			// }

			return groupView.get(groupPosition);
		}
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		Log.e("click ad", childPosition + "");
		return true;
	}

	@Override
	public boolean areAllItemsEnabled() {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.BaseExpandableListAdapter#onGroupCollapsed(int)
	 */
	@Override
	public void onGroupCollapsed(int groupPosition) {
		// TODO Auto-generated method stub
		super.onGroupCollapsed(groupPosition);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.BaseExpandableListAdapter#onGroupExpanded(int)
	 */
	@Override
	public void onGroupExpanded(int groupPosition) {
		// TODO Auto-generated method stub
		super.onGroupExpanded(groupPosition);
	}

	@SuppressWarnings("unused")
	private Handler handler;
	// private Message msg = new Message();
	private static final int ADD_NEW_IMGNAME = 11111;

	private void intHandler() {

		handler = new Handler() {

			@SuppressWarnings("unchecked")
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				switch (msg.what) {
				case ADD_NEW_IMGNAME:

					if (cacheUtil == null) {
						cacheUtil = new ImageListCache(context,
								(ArrayList<Object[]>) msg.obj);
					} else {
						cacheUtil.addToLine((ArrayList<Object[]>) msg.obj);
					}

					break;

				default:

					break;
				}
			}
		};
	}

	public void stopImageThread() {
		cacheUtil.stop();
	}

	public boolean isCacheUtilNull() {
		if (cacheUtil == null) {
			return true;
		} else {
			return false;
		}
	}

}
